import http from '../../../http/http'
import * as url from '../../../http/api'
const state = {
    dishData:[],
    dishTypeData:[],
    pageSize:0,
}
const actions = {
    getDishData({commit},obj){
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodManagementPage,
                method:'post',
                data:obj, 
            }).then(res=>{
                console.log(res.data,'getDishData')
                if(res.data.status == 'success'){
                    commit('saveDish',res.data);
                    resolve('success')
                }else{
                    resolve('error')
                }    
            })
        })          
    },
    getTypeData({commit}){
        http({
            url:url.foodManagementAllDishType,
            method:'post',
        }).then(res=>{
            console.log(res,'getTypeData')
            if(res.data.status == 'success'){
                commit('saveType',res.data.getDishesClassifyOfNameList)
            }    
        }).catch(error=>{
            console.log(error)
        })
    },
    searchDishName({commit},obj){
        console.log(obj,'我要查询的数据')
        
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodManagementSelect,
                method:'post',
                data:obj,
            }).then(res=>{
                console.log(res,'searchDishName')
                if(res.data.status == 'success'){
                    commit('saveSearchDish',res.data);
                    resolve('success')
                }else{
                    resolve('error')
                }   
            })
        })     
    },
    addDish({commit},obj){
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodManagementAdd,
                method:'post',
                data:obj,
                headers:{'Content-Type':'multipart/form-data'},
            }).then(res=>{
                console.log(res,'addDish')
                if(res.data.status == 'success'){
                   resolve('success')
                }else{
                    resolve('error')
                }
            })       
        })
         
    },
    editDish({commit},obj){
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodManagementUpdate,
                method:'post',
                data:obj,
                headers:{'Content-Type':'multipart/form-data'},
            }).then(res=>{
                console.log(res,'editDish')
                if(res.data.status == 'success'){
                    resolve('success')
                }else{
                    resolve('error')
                }    
            })
        })
        
    },
    deleteDish({commit},obj){
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodManagementDelete,
                method:'post',
                data:obj,
            }).then(res=>{
                if(res.data.status == 'success'){
                   resolve('success')
                }else{
                    resolve('error')
                }    
            })
        })
        
    },
}
const mutations = {
    saveDish(state,data){
        state.dishData = data.listDishes;
        state.pageSize = data.totalPage;

    },
    saveSearchDish(state,data){
        state.dishData = data.dishesList;
        state.pageSize = data.totalPage;
    },
    saveType(state,data){
        state.dishTypeData = data;
    }
}
const getters = {

}
export default{
    namespaced: true,
    state,
    actions,
    mutations,
    getters,
}